﻿/**
 * 进度提示框
 */
function Progress() {
    'use strict';
    //处理进度框
    this.progressEle = $('#progressContainer');
}

Progress.prototype = {
    constructor: Progress,
    init: function () {
        this.progressEle.height(document.body.clientHeight);
        this.progressEle.hide();
    },
    //显示处理进度框
    show: function () {
        this.progressEle.show();
    },
    //隐藏处理进度框
    hide: function (progressCount) {
        var self = this;
        var intervalId = setInterval(function (portCount) {
            var completeCount = 0;
            self.progressEle.children().each(function (index, element) {
                var resultCount = $(element).attr('data-resultCount');
                if (resultCount) {
                    completeCount += parseInt(resultCount);
                }
            });

            //全部处理完成时，隐藏进度框并清除子元素
            if (completeCount == portCount) {
                clearInterval(intervalId);
                self.progressEle.fadeOut(2000, function () {
                    self.progressEle.children().remove();
                });
            }
        }, 100, progressCount);
    },
    //向进度框中添加信息
    appendInfo: function (destPortId, message) {
        this.progressEle.append('<h2 class="progressItem" data-destPortId="' + destPortId + '">' + message + '</h2>');
    },
    //更新进度框中的信息
    updateInfo: function (destPortId, message, result) {
        var childs = this.progressEle.children();
        for (var i = 0; i < childs.length; i++) {
            var childEle = $(childs.get(i));
            if (childEle.attr('data-destPortId') == destPortId) {
                childEle.text(message);
                childEle.attr('data-resultCount', 1);
                childEle.addClass(result == '成功' ? 'progressItem-success' : 'progressItem-error');
                break;
            }
        }
    },
    //判断destPortId的处理是否完成
    isComplete: function (destPortId) {
        var childs = this.progressEle.children();
        for (var i = 0; i < childs.length; i++) {
            var childEle = $(childs.get(i));
            if (childEle.attr('data-destPortId') == destPortId) {
                return childEle.hasClass('progressItem-success') || childEle.hasClass('progressItem-error');
            }
        }
        return false;
    }
};